package org.kimoji.base;

import java.util.Calendar;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.kimoji.entity.User;
import org.kimoji.entity.biz.JsonRes;
import org.kimoji.security.entity.Account;
import org.kimoji.security.service.SecurityService;
import org.kimoji.security.tool.SecurityTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

public class BaseController {

	@Autowired
	private SecurityService securityService;
	
	/**
	 * 授權
	 * <strong>Description</strong>:
	 * @param jsonRes
	 * @param session
	 * @param response
	 * @return void
	 * @author chok
	 * 2014-2-3
	 */
	protected void auth(String token,HttpSession session,HttpServletResponse response){
		if(null != token){
			Cookie auth = new Cookie("token", token);
			auth.setPath("/");
			auth.setMaxAge(900000);
			response.addCookie(auth);
			session.setAttribute("token", token);
		}
	}
	
	/**
	 * 根據授權獲取個人信息
	 * <strong>Description</strong>:
	 * @param session
	 * @param request
	 * @return
	 * @return User
	 * @author chok
	 * 2014-2-3
	 */
	protected String getAuth(HttpSession session,Cookie cookie){
		String token = (String)session.getAttribute("token");
		if(null == token){
			token = cookie.getValue();
		if(null == token)
			return null;
		}
		return securityService.identify(token);
	}
	
}
